package com.liveneo.adapter.mobile.service.impl;

import java.util.Map;

import org.springframework.stereotype.Service;

import com.google.common.collect.Maps;
import com.liveneo.adapter.base.support.BaseServiceImpl;
import com.liveneo.adapter.base.support.LogUtils;
import com.liveneo.adapter.mobile.model.request.AddLossItemRequest;
import com.liveneo.adapter.mobile.model.request.AssessLossItemRequest;
import com.liveneo.adapter.mobile.model.request.CallbackMsgRequest;
import com.liveneo.adapter.mobile.model.request.CaseListRequest;
import com.liveneo.adapter.mobile.model.request.LoginRequest;
import com.liveneo.adapter.mobile.model.request.LossItemRequest;
import com.liveneo.adapter.mobile.model.request.LossReassignRequest;
import com.liveneo.adapter.mobile.model.request.MsgListRequest;
import com.liveneo.adapter.mobile.model.request.OcrPayeeInfoRequest;
import com.liveneo.adapter.mobile.model.request.UpdateStatesRequest;
import com.liveneo.adapter.mobile.model.request.VersionRequest;
import com.liveneo.adapter.mobile.service.ValidateService;
import com.liveneo.adapter.mobile.support.MobileReturnCodeSupport;
import com.liveneo.system.base.utils.AppException;

/**
 * 业务的校验处理类
 * 
 * @author wangxx
 * @since 2016-06-08
 */
@Service
public class ValidateServiceImpl extends BaseServiceImpl implements ValidateService {
    @Override
    public Map<String, Object> validateLogin(LoginRequest request) {
        Map<String, Object> result = Maps.newHashMap();
        String userCode = request.getUserCode();
        String password = request.getPassword();
        String fromPlat = request.getFromPlat();
        String phoneType = request.getPhoneType();
        String os = request.getOs();
        if (userCode == null || "".equals(userCode)) {
            // 手机号为空
            LogUtils.warn("手机端登陆时 登陆名为空！");
            result.put("result", 2);
            result.put("code", MobileReturnCodeSupport.CODE_NOVALID_PARAM);
            result.put("detailCode", MobileReturnCodeSupport.CODE_PARAM_NO_USERCODE);
        } else if ((password == null || "".equals(password)) && !"1".equals(fromPlat)) {
            // 密码为空 且非创盈平台
            LogUtils.warn("手机端登陆时 非创盈平台 密码为空！");
            result.put("result", 2);
            result.put("code", MobileReturnCodeSupport.CODE_NOVALID_PARAM);
            result.put("detailCode", MobileReturnCodeSupport.CODE_PARAM_NO_PASSWORD);
        } else if ((phoneType == null || "".equals(phoneType)) && (os == null || "".equals(os))) {
            // 密码为空 且非创盈平台
            LogUtils.warn("参数不存在终端类型！");
            result.put("result", 2);
            result.put("code", MobileReturnCodeSupport.CODE_NOVALID_PARAM);
            result.put("detailCode", MobileReturnCodeSupport.CODE_PARAM_NO_PHONETYPE);
        } else {
            result.put("result", 1);
        }
        return result;
    }

    @Override
    public void validateGetCaseList(CaseListRequest request) throws AppException {
        // String surveyUserCode = request.getSurveyUserCode();
        // if (surveyUserCode == null || "".equals(surveyUserCode)) {
        // throw new AppException(MobileReturnCodeSupport.CODE_NOVALID_PARAM,
        // MobileReturnCodeSupport.CODE_PARAM_NO_USERCODE);
        // }
        String pageNo = request.getPageNo();
        String pageSize = request.getPageSize();
        if (pageNo == null || "".equals(pageNo) || pageSize == null || "".equals(pageSize)) {
            throw new AppException(MobileReturnCodeSupport.CODE_NOVALID_PARAM,
                    MobileReturnCodeSupport.CODE_PARAM_NO_PAGEINFO);
        }
    }

    @Override
    public void validateGetLossItemList(LossItemRequest request) throws AppException {
        String caseUuid = request.getCaseUuid();
        if (caseUuid == null || "".equals(caseUuid)) {
            throw new AppException(MobileReturnCodeSupport.CODE_NOVALID_PARAM,
                    MobileReturnCodeSupport.CODE_PARAM_NO_CASEUUID);
        }
        String surveyUserCode = request.getSurveyUserCode();
        if (surveyUserCode == null || "".equals(surveyUserCode)) {
            throw new AppException(MobileReturnCodeSupport.CODE_NOVALID_PARAM,
                    MobileReturnCodeSupport.CODE_PARAM_NO_USERCODE);
        }
        String pageNo = request.getPageNo();
        String pageSize = request.getPageSize();
        if (pageNo == null || "".equals(pageNo) || pageSize == null || "".equals(pageSize)) {
            throw new AppException(MobileReturnCodeSupport.CODE_NOVALID_PARAM,
                    MobileReturnCodeSupport.CODE_PARAM_NO_PAGEINFO);
        }
    }

    @Override
    public void valicateGetAssessLossItem(AssessLossItemRequest request) throws AppException {
        String userCode = request.getSurveyUserCode();
        if (userCode == null || "".equals(userCode)) {
            throw new AppException(MobileReturnCodeSupport.CODE_NOVALID_PARAM,
                    MobileReturnCodeSupport.CODE_PARAM_NO_USERCODE);
        }
        String caseNo = request.getCaseNo();
        if (caseNo == null || "".equals(caseNo)) {
            throw new AppException(MobileReturnCodeSupport.CODE_NOVALID_PARAM,
                    MobileReturnCodeSupport.CODE_PARAM_NO_CASENO);
        }
    }

    @Override
    public void validateGetAssessLossItem(AssessLossItemRequest request) throws AppException {
        String surveyUserCode = request.getSurveyUserCode();
        if (surveyUserCode == null || "".equals(surveyUserCode)) {
            throw new AppException(MobileReturnCodeSupport.CODE_NOVALID_PARAM,
                    MobileReturnCodeSupport.CODE_PARAM_NO_USERCODE);
        }
    }

//    @Override
//    public void validateGetVersion(VersionRequest request) throws AppException {
//        String versionCode = request.getVersionCode();
//        String versionId = request.getVersionId();
//        if (versionCode == null && versionId == null) {
//            throw new AppException(MobileReturnCodeSupport.CODE_NOVALID_PARAM,
//                    MobileReturnCodeSupport.CODE_PARAM_NO_VERSIONCODE);
//        }
//        String appType = request.getOs();
//        String os = request.getOs();
//        if (appType == null && os == null) {
//            throw new AppException(MobileReturnCodeSupport.CODE_NOVALID_PARAM,
//                    MobileReturnCodeSupport.CODE_PARAM_NO_OS);
//        }
//        String phoneNo = request.getTel();
//        String tel = request.getTel();
//        if (phoneNo == null && tel == null) {
//            throw new AppException(MobileReturnCodeSupport.CODE_NOVALID_PARAM,
//                    MobileReturnCodeSupport.CODE_PARAM_NO_PHONENO);
//        }
//        String phoneModel = request.getPhoneModel();
//        if (phoneModel == null) {
//            throw new AppException(MobileReturnCodeSupport.CODE_NOVALID_PARAM,
//                    MobileReturnCodeSupport.CODE_PARAM_NO_PHONEMODEL);
//        }
//        String systemInfo = request.getSystemInfo();
//        if (systemInfo == null) {
//            throw new AppException(MobileReturnCodeSupport.CODE_NOVALID_PARAM,
//                    MobileReturnCodeSupport.CODE_PARAM_NO_SYSTEMINFO);
//        }
//        String systemCode = request.getSystemCode();
//        if (systemCode == null) {
//            throw new AppException(MobileReturnCodeSupport.CODE_NOVALID_PARAM,
//                    MobileReturnCodeSupport.CODE_PARAM_NO_SYSTEMCODE);
//        }
//    }

    @Override
    public void validateAddPayeeInfo(OcrPayeeInfoRequest request) throws AppException {
        // String mobile = request.getMobile();
        // if (mobile == null){
        // throw new AppException(MobileReturnCodeSupport.CODE_NOVALID_PARAM,
        // MobileReturnCodeSupport.CODE_PARAM_NO_PHONENO);
        // }
        String lossUuid = request.getLossUuid();
        if (lossUuid == null) {
            throw new AppException(MobileReturnCodeSupport.CODE_NOVALID_PARAM,
                    MobileReturnCodeSupport.CODE_PARAM_NO_LOSSUUID);
        }
        String caseUuid = request.getCaseUuid();
        if (caseUuid == null) {
            throw new AppException(MobileReturnCodeSupport.CODE_NOVALID_PARAM,
                    MobileReturnCodeSupport.CODE_PARAM_NO_CASEUUID);
        }
        String caseNo = request.getCaseNo();
        if (caseNo == null) {
            throw new AppException(MobileReturnCodeSupport.CODE_NOVALID_PARAM,
                    MobileReturnCodeSupport.CODE_PARAM_NO_CASENO);
        }
        // String surveyUserCode = request.getSurveyUserCode();
        // if (surveyUserCode == null){
        // throw new AppException(MobileReturnCodeSupport.CODE_NOVALID_PARAM,
        // MobileReturnCodeSupport.CODE_PARAM_NO_USERCODE);
        // }
    }

    @Override
    public void validateCallbackMsg(CallbackMsgRequest request) throws AppException {
        String uuid = request.getMessageUuid();
        if (uuid == null) {
            throw new AppException(MobileReturnCodeSupport.CODE_NOVALID_PARAM,
                    MobileReturnCodeSupport.CODE_PARAM_NO_UUID);
        }
        String receiveTime = request.getReceiveTime();
        if (receiveTime == null) {
            throw new AppException(MobileReturnCodeSupport.CODE_NOVALID_PARAM,
                    MobileReturnCodeSupport.CODE_PARAM_NO_RECEIVETIME);
        }
    }

    @Override
    public void validateMsgList(MsgListRequest request) throws AppException {
        String pageNo = request.getPageNo();
        String pageSize = request.getPageSize();
        if (pageNo == null || "".equals(pageNo) || pageSize == null || "".equals(pageSize)) {
            throw new AppException(MobileReturnCodeSupport.CODE_NOVALID_PARAM,
                    MobileReturnCodeSupport.CODE_PARAM_NO_PAGEINFO);
        }
        String receiveName = request.getReceiveName();
        if (receiveName == null) {
            throw new AppException(MobileReturnCodeSupport.CODE_NOVALID_PARAM,
                    MobileReturnCodeSupport.CODE_PARAM_NO_RECEIVENAME);
        }
    }

    @Override
    public void validateMsgUpdateStatus(UpdateStatesRequest request) throws AppException {
        String msgUuids = request.getMessageUuids();
        if (msgUuids == null) {
            throw new AppException(MobileReturnCodeSupport.CODE_NOVALID_PARAM,
                    MobileReturnCodeSupport.CODE_PARAM_NO_UUID);
        }
        String operatorType = request.getOperatorType();
        if (operatorType == null) {
            throw new AppException(MobileReturnCodeSupport.CODE_NOVALID_PARAM,
                    MobileReturnCodeSupport.CODE_PARAM_NO_OPERATORTYPE);
        }
    }

    @Override
    public void validateLossReassign(LossReassignRequest request) throws AppException {
        String lossUuids = request.getLossUuids();
        if (lossUuids == null || "".equals(lossUuids)) {
            throw new AppException(MobileReturnCodeSupport.CODE_NOVALID_PARAM,
                    MobileReturnCodeSupport.CODE_PARAM_NO_LOSSUUID);
        }
        String surveyUserCode = request.getSurveyUserCode();
        if (surveyUserCode == null || "".equals(surveyUserCode)) {
            throw new AppException(MobileReturnCodeSupport.CODE_NOVALID_PARAM,
                    MobileReturnCodeSupport.CODE_PARAM_NO_USERCODE);
        }
        String applyScheReason = request.getApplyScheReason();
        if (applyScheReason == null || "".equals(applyScheReason)) {
            throw new AppException(MobileReturnCodeSupport.CODE_NOVALID_PARAM,
                    MobileReturnCodeSupport.CODE_PARAM_NO_REASSIGNREASON);
        }
        String applySchePlat = request.getApplySchePlat();
        if (applySchePlat == null || "".equals(applySchePlat)) {
            throw new AppException(MobileReturnCodeSupport.CODE_NOVALID_PARAM,
                    MobileReturnCodeSupport.CODE_PARAM_NO_REASSIGNPLAT);
        }
        String applyScheType = request.getApplyScheType();
        if (applyScheType == null || "".equals(applyScheType)) {
            throw new AppException(MobileReturnCodeSupport.CODE_NOVALID_PARAM,
                    MobileReturnCodeSupport.CODE_PARAM_NO_REASSIGNTYPE);
        }
    }

    @Override
    public void validateAddLossItem(AddLossItemRequest request) throws AppException {
        String caseNo = request.getCaseNo();
        if (caseNo == null || "".equals(caseNo)) {
            throw new AppException(MobileReturnCodeSupport.CODE_NOVALID_PARAM,
                    MobileReturnCodeSupport.CODE_PARAM_NO_CASENO);
        }
        String lossName = request.getLossName();
        if (lossName == null || "".equals(lossName)) {
            throw new AppException(MobileReturnCodeSupport.CODE_NOVALID_PARAM,
                    MobileReturnCodeSupport.CODE_PARAM_NO_LOSSNAME);
        }
        String lossType = request.getLossType();
        if (lossType == null || "".equals(lossType)) {
            throw new AppException(MobileReturnCodeSupport.CODE_NOVALID_PARAM,
                    MobileReturnCodeSupport.CODE_PARAM_NO_LOSSTYPE);
        }
        String surveyUserCode = request.getSurveyUserCode();
        if (surveyUserCode == null || "".equals(surveyUserCode)) {
            throw new AppException(MobileReturnCodeSupport.CODE_NOVALID_PARAM,
                    MobileReturnCodeSupport.CODE_PARAM_NO_USERCODE);
        }
    }
}
